﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.FireProjects.BLL;
using Yipusoft.FireProjects.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using System.IO;

public partial class BasicBase_FireProject_FireProjectList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {

        if (Request.QueryString["where"] != null)
        {

            lblwhere.Text = Request.QueryString["where"];
            Bind(0);
            topmenu.Visible = false;
            GV.Columns[3].Visible = false;
            GV.Columns[4].Visible = false;
            GV.Columns[6].Visible = false;
        }
        else
        {
            string strWhere = " ParentID='MHJYYALB0000' ";
            List<CodeItemInfo> list = CodeItem.GetList(strWhere);
            ddlTypeID.Items.Add("");
            for (int i = 0; i < list.Count; i++)
            {
                ddlTypeID.Items.Add(new ListItem(list[i].ItemName, list[i].ItemID));
            }


            if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201301"))//总队级
            {

            }
            else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201302"))//支队级
            {
                UCDepartmentISEmp1.DepartmentEnabled = false;
                UCDepartmentISEmp1.UnitListEnabled = true;
            }
            else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201310"))//中队级
            {
                UCDepartmentISEmp1.DepartmentEnabled = false;
                UCDepartmentISEmp1.UnitListEnabled = false;
                UCDepartmentISEmp1.UnitEnable = false;
            }

        }
    }

    void BindDDlClassid()
    {
        if (ddlTypeID.SelectedValue != "")
        {
            ddlClassID.Items.Clear();
            ddlClassID.Items.Add("");
            List<CodeItemInfo> list = CodeItem.GetList(" ParentID='" + ddlTypeID.Text + "' ");

            for (int i = 0; i < list.Count; i++)
            {
                ddlClassID.Items.Add(new ListItem(list[i].ItemName, list[i].ItemID));
            }
        }
        else
        {
            ddlClassID.Items.Clear();
        }
    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(PageIndex);
    }
    protected void lbtQurey_Click(object sender, EventArgs e)
    {

        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201300") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201305"))
        {


            string strWhere = "";
            if (UCDepartmentISEmp1.GetDepartmentID() != "")
            {
                strWhere += " DepartmentID='" + UCDepartmentISEmp1.GetDepartmentID() + "' ";
            }
            else
            {
                if (UCDepartmentISEmp1.GetParentDepartment() != "")
                {
                    strWhere += " (DepartmentID like '1300%' or DepartmentID like '" + UCDepartmentISEmp1.GetParentDepartment().Substring(0, 4) + "%')";
                }
                else
                {
                    string Departmentid = Yipusoft.Account.BLL.User.GetUser(Page.User.Identity.Name).DepartmentID;
                    if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201301"))//总队级
                    {

                    }
                    else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201302"))//支队级
                    {
                        strWhere += "( DepartmentID like '1300%' or DepartmentID like '" + Departmentid.Substring(0, 4) + "%')";
                    }
                    else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201310"))//中队级
                    {
                        strWhere += " DepartmentID like '" + Departmentid.Substring(0, 4) + "%'";
                    }
                    else if (Yipusoft.Account.BLL.User.HasPermission(Page.User.Identity.Name, "201303"))//本单位级
                    {

                    }
                }
            }
            if (ddlTypeID.SelectedValue != "")
            {
                if (strWhere.Length != 0)
                {
                    strWhere += " and ";
                }

                strWhere += " TypeID='" + ddlTypeID.SelectedValue + "' ";

            }
            if (ddlClassID.SelectedValue != "")
            {
                if (strWhere.Length != 0)
                {
                    strWhere += " and ";
                }
                strWhere += " ClassID='" + ddlClassID.SelectedValue + "' ";
            }
            if (ProjectName.Text != "")
            {
                if (strWhere.Length != 0)
                {
                    strWhere += " and ";
                }
                strWhere += " ProjectName like '%" + ProjectName.Text + "%' ";
            }
            lblwhere.Text = strWhere;
            ListPageContral1.SetFirstPage();
            Qurey();
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }
    }

    void Qurey()
    {

        Bind(0);
    }

    void Bind(int pageIndex)
    {
        int count = FireProject.GetCount(lblwhere.Text);
        ListPageContral1.ItemCount = count;
        GV.DataSource = FireProject.GetList(ListPageContral1.PageSize, pageIndex, lblwhere.Text);
        GV.DataBind();
    }
    protected void gvChemical_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(GV.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            FireProjectInfo info = e.Row.DataItem as FireProjectInfo;
            string Typeid = info.TypeID;
            if (Typeid != "")
            {
                Label lblType = e.Row.FindControl("lblType") as Label;

                lblType.Text = CodeItem.GetCodeItem(Typeid).ItemName;
            }
            string Classid = info.ClassID;
            if (Classid != "")
            {
                Label lblClass = e.Row.FindControl("lblClass") as Label;
                lblClass.Text = CodeItem.GetCodeItem(Classid).ItemName;
            }

            string Departmentid = info.DepartmentID;
            if (Departmentid != "")
            {
                Label lblDepartment = e.Row.FindControl("lblDepartment") as Label;
                lblDepartment.Text = Department.GetDepartment(Departmentid).FullName;
            }
        }
    }
    protected void gvChemical_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201300") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201309"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            string path = Server.MapPath(".\\WordFile\\");

            FireProjectInfo info = FireProject.getFireproject(new Guid(e.CommandArgument.ToString()));
            path += info.WordName;
            //删除记录
            Yipusoft.FireProjects.BLL.FireProject.Delete(new Guid(e.CommandArgument.ToString()));


            //删除文件
            FileInfo file = new FileInfo(path);
            file.IsReadOnly = false;
            file.Delete();
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "提示:删除一个灾火救援预案!";
            log.ItemID = e.CommandArgument.ToString();
            log.ItemName = "BasicBase";
            log.ItemFullUrl = "FirePrjoje/ChemicalList";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
            Prompt.Text = "提示:删除成功!";
            Bind(0);
        }
    }
    protected void ddlTypeID_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDDlClassid();
    }
}
